/**
 * @description Demonstrates various ways of invoking a DataWeave scripts
 * in Apex. Note that this class demonstrates both static and dynamic way of
 * invoking DataWeave scripts.
 * @group DataWeaveInApex Recipes
 */
public with sharing class BasicDataWeaveInApexRecipes {
    /**
     * @description Invokes a DataWeave script in Apex using static invocation.
     * Uses the `/dw/helloWorld.dwl` DataWeave script to log a message.
     * This shows how to invoke the script using the Apex class that is automatically
     * generated by the platform from the DataWeave script. The DataWeave script is deployed as metadata.
     * @return output of the DataWeave script as a string (hard coded to "Hello World")
     */
    public static String staticInvocation() {
        DataWeave.Script script = new DataWeaveScriptResource.helloWorld();
        DataWeave.Result result = script.execute(new Map<String, Object>());
        return result.getValueAsString();
    }

    /**
     * @description Invokes a DataWeave script in Apex using dynamic invocation thanks to `DataWeave.Script.createScript()`.
     * @param scriptName name of the DataWeave metadata to invoke
     * @return output of the DataWeave script as a string
     */
    public static String dynamicInvocation(String scriptName) {
        DataWeave.Script script = DataWeave.Script.createScript(scriptName);
        DataWeave.Result result = script.execute(new Map<String, Object>());
        return result.getValueAsString();
    }
}
